package cn.com.dhc.danlu.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import java.util.LinkedList;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.DLToolbar;

/**
 * @author wuhaiyang
 */
@SuppressLint("Registered")
@Deprecated
public class FragmentStackActivity extends DefaultFragmentActivity {
    private static final String TAG_DEFAULT_FRAG = "default_fragment";
    protected final LinkedList<String> mTitles = new LinkedList<>();
    protected FragmentManager mFragmentManager;
    protected DLToolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentManager = getSupportFragmentManager();
    }

    protected void addDefaultFragment(Fragment fragment, String title) {
        addDefaultFragment(fragment, R.id.fl_fragment_container, title);
    }

    /**
     * 对于toolbar title 可以维护一个 manager
     */
    protected void addDefaultFragment(Fragment fragment, int fragmentLayoutId, String title) {
        if (null == fragment) {
            throw new RuntimeException("please init your fragment");
        }
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.add(fragmentLayoutId, fragment, TAG_DEFAULT_FRAG);
        transaction.commit();
        if (!TextUtils.isEmpty(title)) {
            mTitles.add(title);
        }
        mToolbar.setTitle(mTitles.get(0));
    }

    protected void switchFragment(Fragment targetFragment, String title) {
        switchFragment(targetFragment, R.id.fl_fragment_container, title);
    }

    protected void switchFragment(Fragment targetFragment, int fragmentLayoutId, final String title) {
        if (null == targetFragment) {
            throw new RuntimeException("please init your fragment");
        }
        Fragment fragmentByTag = mFragmentManager.findFragmentByTag(TAG_DEFAULT_FRAG);
        if (null != fragmentByTag && targetFragment == fragmentByTag) {
            return;
        }
        mFragmentManager.beginTransaction()
                .setCustomAnimations(R.anim.slide_right_in, 0, 0, R.anim.slide_left_exit)
                .addToBackStack(targetFragment.getClass().getSimpleName()).replace(fragmentLayoutId, targetFragment)
                .commit();
        mToolbar.postDelayed(new Runnable() {
            @Override
            public void run() {
                mToolbar.setBackVisible(true);
                mToolbar.setTitle(title);
            }
        }, 300);
        if (!TextUtils.isEmpty(title)) {
            mTitles.add(title);
        }
    }

    /**
     * 重写按下左上角返回键的 的时候切换fragment
     */
    @Override
    protected void backPressed() {
        popFragment();
    }

    protected void popFragment() {
        int stackEntryCount = mFragmentManager.getBackStackEntryCount();
        if (stackEntryCount >= 1) {
            getSupportFragmentManager().popBackStack();
            changeTitle(stackEntryCount);
        } else {
            finish();
        }
    }

    protected void changeTitle(int stackCount) {
        mTitles.removeLast();
        String s = mTitles.get(stackCount - 1);
        mToolbar.setTitle(s);
    }

    /**
     * 按下物理返回键的时候回调该方法
     */
    @Override
    public void onBackPressed() {
        popFragment();
    }
}